package assist.client.gwt.view.mvp;

import com.google.gwt.activity.shared.Activity;
import com.google.gwt.activity.shared.ActivityMapper;
import com.google.gwt.place.shared.Place;
import assist.client.gwt.view.ClientFactory;
import assist.client.gwt.view.activity.SelectGamesActivity;
import assist.client.gwt.view.activity.ShowStatsActivity;
import assist.client.gwt.view.activity.SelectStatsActivity;
import assist.client.gwt.view.event.EditGamesEvent;
import assist.client.gwt.view.event.EditGamesEventHandler;
import assist.client.gwt.view.place.SelectGamesPlace;
import assist.client.gwt.view.place.ShowStatsPlace;
import assist.client.gwt.view.place.SelectStatsPlace;

public class AppActivityMapper implements ActivityMapper, EditGamesEventHandler
{

	private ClientFactory clientFactory;

	/**
	 * AppActivityMapper associates each Place with its corresponding
	 * {@link Activity}
	 * 
	 * @param clientFactory
	 *            Factory to be passed to activities
	 */
	public AppActivityMapper(ClientFactory clientFactory) {
		super();
		this.clientFactory = clientFactory;
        this.clientFactory.getEventBus().addHandler(EditGamesEvent.TYPE, this);
	}

	/**
	 * Map each Place to its corresponding Activity. This would be a great use
	 * for GIN.
	 */
	@Override
	public Activity getActivity(Place place) {	    
		// This is begging for GIN
		if (place instanceof ShowStatsPlace)
			return new ShowStatsActivity((ShowStatsPlace) place, clientFactory);
		else if (place instanceof SelectStatsPlace)
			return new SelectStatsActivity((SelectStatsPlace) place, clientFactory);
		else if (place instanceof SelectGamesPlace)
		    return new SelectGamesActivity((SelectGamesPlace) place, clientFactory);
		return null;
	}

    @Override
    public void onEditGamesMode(boolean mode)
    {
        this.clientFactory.getShowGamesView().setEditMode(mode);
    }

}
